<!--
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-06-19 14:29:37
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-06-19 14:29:38
 * @FilePath: /sjf/2024-project/2024_centos/gobang/wwwroot/login.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
</head>

<body>
    <div class="nav">
        ⽹络五⼦棋对战游戏
    </div>
    <div class="login-container">
        <!-- 登录界⾯的对话框 -->
        <div class="login-dialog">
            <!-- 提⽰信息 -->
            <h3>登录</h3>
            <!-- 这个表⽰⼀⾏ -->
            <div class="row">
                <span>⽤⼾名</span>
                <input type="text" id="user_name">
            </div>
            <!-- 这是另⼀⾏ -->
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <!-- 提交按钮 -->
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>
            <div class="row">
                <p>还没有账号? <a href="./register.html">点击注册</a></p>
            </div>
        </div>
    </div>
    <script src="./js/jquery.min.js"></script>
    <script>
        //1. 给按钮添加点击事件，调⽤登录请求函数
        //2. 封装登录请求函数
        function login() {
            // 1. 获取输⼊框中的⽤⼾名和密码，并组织json对象
            var login_info = {
                username: document.getElementById("user_name").value,
                password: document.getElementById("password").value
            };
            // 2. 通过ajax向后台发送登录验证请求
            $.ajax({
                url: "/login",
                type: "post",
                data: JSON.stringify(login_info),
                success: function (result) {
                    // 3. 如果验证通过，则跳转游戏⼤厅⻚⾯
                    alert("登录成功");
                    window.location.assign("/game_hall.html");
                },
                error: function (xhr) {
                    // 4. 如果验证失败，则提⽰错误信息，并清空输⼊框
                    alert(JSON.stringify(xhr));
                    document.getElementById("user_name").value = "";
                    document.getElementById("password").value = "";
                }
            })
        }
    </script>
</body>
</html>